<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
    var planWebsocket = new WebSocket('wss://'+ window.location.hostname +':' + window.location.port + '/');

    planWebsocket.onopen = function OnOpen(params) {
        
    }
    planWebsocket.onmessage = function OnMessage(Message) {
        
    }
    planWebsocket.onerror = function OnError(errorMsg) {
        
    }
    function Publish() {

        var self = {};
        // https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
        self.constraints = {
            audio: true,
            video: {
                width: {ideal: 320, max: 576}
            }
        };
        var pc = new RTCPeerConnection
        pc.addTransceiver("audio", {direction: "sendonly"});
        pc.addTransceiver("video", {direction: "sendonly"});

        self.pc = pc;

        if (!navigator.mediaDevices && window.location.protocol === 'http:' && window.location.hostname !== 'localhost') {
            throw new SrsError('HttpsRequiredError', `Please use HTTPS or localhost to publish, read https://github.com/ossrs/srs/issues/2762#issuecomment-983147576`);
        }
        var stream = navigator.mediaDevices.getUserMedia(self.constraints);

        // stream.getTracks().forEach(function (track) {

        //     self.pc.addTrack(track);
        //     // Notify about local track when stream is ok.
        //     self.ontrack && self.ontrack({track: track});
        // });
        var offer = self.pc.createOffer();
        self.pc.setLocalDescription(offer);
        var session =  new Promise(function (resolve, reject) {
            // @see https://github.com/rtcdn/rtcdn-draft
            var data = {
                api: "www", tid: "id", streamurl: "stream_url",
                clientip: null, sdp: offer.sdp
            };
            console.log("Generated offer: ", data);

            $.ajax({
                type: "POST", url: "www", data: JSON.stringify(data),
                contentType: 'application/json', dataType: 'json'
            }).done(function (data) {
                console.log("Got answer: ", data);
                if (data.code) {
                    reject(data);
                    return;
                }

                resolve(data);
            }).fail(function (reason) {
                reject(reason);
            });
        });
        self.pc.setRemoteDescription(
            new RTCSessionDescription({type: 'answer', sdp: session.sdp})
        );
        // session.simulator = conf.schema + '//' + conf.urlObject.server + ':' + conf.port + '/rtc/v1/nack/';

        return session;
    }
    function PlayStream() {


    }
    function OnMessage() {

    }
</script>
<body>
    <video>
        
    </video>
    <input type="button" value="START" onclick="Publish()">
</body>
</html>